
import 'package:spring_dict/EventBus/EventBus.dart';
import 'package:test/test.dart';

void main(){
  test("EventBus", (){

    var app_onceTicker = 0;
    var app_everyTimeTicker = 0;

    var example_onceTicker = 0;
    var example_everyTimeTicker = 0;

    var bus = EventBus.singleton();

    bus.subscribeOnce<MySubject>("app", (bus, data){
      expect(data.name, "yeganaaa");
      app_onceTicker++;
    });

    bus.subscribeOnce<MySubject>("example", (bus, data){
      expect(data.name, "yeganaaa");
      example_onceTicker++;
    });

    bus.subscribe<MySubject>("app", (bus, data){
      expect(data.description, "person");
      app_everyTimeTicker++;

      data.description = "description changed!";
    });

    bus.subscribe<MySubject>("example", (bus, data){
      expect(data.description, "person");
      example_everyTimeTicker++;
    });

    var app_person = bus.publish("app", MySubject(
        name: "yeganaaa",
        description: "person"
    ));
    expect(app_person.description, "description changed!");
    bus.publish("app", MySubject(
        name: "yeganaaa",
        description: "person"
    ));

    bus.subscribe<MySubject>("error", (bus, data){
      expect(false, true);
    });

    bus.publish("example", MySubject(
        name: "yeganaaa",
        description: "person"
    ));
    bus.publish("example", MySubject(
        name: "yeganaaa",
        description: "person"
    ));

    expect(app_onceTicker, 1);
    expect(app_everyTimeTicker, 2);

    expect(example_onceTicker, 1);
    expect(example_everyTimeTicker, 2);

    EventBus.singleton()
    ..unSubscribeAll("app.example")
    ..publish("app.example", MySubject(
        name: "yeganaaa",
        description: "person"
    ));

    expect(app_onceTicker, 1);
    expect(app_everyTimeTicker, 2);


  });
}



class MySubject {
  String name;
  String description;
  MySubject({this.name, this.description});
}